﻿namespace Home.Test.Repository
{
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;

    public class IPDbContext : DbContext
    {
        static IPDbContext()
        {
            Database.SetInitializer<IPDbContext>(null);
        }

        public IPDbContext()
            : base("Name=DefaultConnection")
        {
        }

        public DbSet<IPClient> Clients { get; set; }
        public DbSet<IPSns> Snses { get; set; }
        public DbSet<IPConsent> Consents { get; set; }

        protected override void OnModelCreating(DbModelBuilder builder)
        {
            // client
            {
                var config = builder.Entity<IPClient>();
                config.ToTable("tt_ip_client");
                config.HasKey(m => m.Id)
                    .Property(m => m.Id)
                    .HasColumnName("id")
                    .HasMaxLength(64)
                    .IsUnicode(false)
                    .IsVariableLength()
                    .IsRequired();
                config.Property(m => m.Secret)
                    .HasColumnName("secret")
                    .HasMaxLength(64)
                    .IsUnicode(false)
                    .IsVariableLength()
                    .IsRequired();
                config.Property(m => m.CallbackUrl)
                    .HasColumnName("callback_uri")
                    .HasMaxLength(128)
                    .IsUnicode(false)
                    .IsVariableLength()
                    .IsRequired();
            }

            // SNS
            {
                var config = builder.Entity<IPSns>();
                config.ToTable("tt_ip_sns");
                config.HasKey(m => m.Id)
                    .Property(m => m.Id)
                    .HasColumnName("id")
                    .IsRequired();
                config.Property(m => m.LoginName)
                    .HasColumnName("login_name")
                    .HasMaxLength(64)
                    .IsUnicode(false)
                    .IsVariableLength()
                    .IsRequired();
                config.Property(m => m.LoginPassword)
                    .HasColumnName("login_password")
                    .HasMaxLength(64)
                    .IsUnicode(false)
                    .IsVariableLength()
                    .IsRequired();
                config.Property(m => m.Nickname)
                    .HasColumnName("nickname")
                    .HasMaxLength(64)
                    .IsUnicode(true)
                    .IsVariableLength()
                    .IsUnicode()
                    .IsRequired();
            }

            // consent
            {
                var config = builder.Entity<IPConsent>();
                config.ToTable("tt_ip_consent");
                config.HasKey(m => m.Id)
                    .Property(m => m.Id)
                    .HasColumnName("id")
                    .IsRequired();
                config.Property(m => m.SnsId)
                    .HasColumnName("sns_id")
                    .IsRequired();
                config.HasRequired(m => m.Sns)
                    .WithRequiredPrincipal();
                config.Property(m => m.ClientId)
                    .HasColumnName("client_id")
                    .HasMaxLength(64)
                    .IsUnicode(false)
                    .IsVariableLength()
                    .IsRequired();
                config.HasRequired(m => m.Client)
                    .WithRequiredPrincipal();
                config.Property(m => m.OpenId)
                    .HasColumnName("open_id")
                    .HasMaxLength(64)
                    .IsUnicode(false)
                    .IsVariableLength()
                    .IsRequired();
            }
        }
    }
}